const Router = require('koa-router')
const jwt = require('koa-jwt')

const router = new Router({ prefix: '/users' })
const {
  find,
  findById,
  create,
  update,
  delete: del,
  login,
  checkOwner,
  listFollowing,
  follow,
  unfollow,
  listFollowers,
  checkUserExist,
  followTopic,
  unfollowTopic,
  listFollowingTopics,
  listQuestions,
  listLikingAnswers,
  likeAnswers,
  unlikeAnswers,
  listDisLikingAnsers,
  dislikeAnswers,
  disunlikeAnswers,
  listCollectingAnswers,
  collectAnswers,
  uncollectAnswers,
} = require('../controllers/users.js')

const { checkTopicExist } = require('../controllers/topics')
const { checkAnswerExist } = require('../controllers/answers')

const { secret } = require('../config.js')

const auth = jwt({ secret })

router.get('/', find)

router.post('/', create)

router.get('/:id', findById)

router.patch('/:id', auth, checkOwner, update)

router.delete('/:id', auth, checkOwner, del)

router.post('/login', login)



router.get('/:id/following', listFollowing)

router.put('/following/:id', auth, checkUserExist, follow)

router.delete('/following/:id', auth, checkUserExist, unfollow)

router.get('/:id/listFollowers', listFollowers)



router.put('/followingTopics/:id', auth, checkTopicExist, followTopic)

router.delete('/followingTopics/:id', auth, checkTopicExist, unfollowTopic)

router.get('/:id/followingTopics', auth, checkTopicExist, listFollowingTopics)



router.get('/:id/questions', listQuestions)



router.get('/:id/likingAnswers', listLikingAnswers)

router.put('/likingAnswers/:id', auth, checkAnswerExist, likeAnswers, dislikeAnswers)

router.delete('/likingAnswers/:id', auth, checkAnswerExist, unlikeAnswers)



router.get('/:id/disLikingAnswers', listDisLikingAnsers)

router.put('/disLikingAnswers/:id', auth, checkAnswerExist, dislikeAnswers, likeAnswers)

router.delete('/disLikingAnswers/:id', auth, checkAnswerExist, disunlikeAnswers)



router.get('/:id/collectingAnswers', listCollectingAnswers)

router.put('/collectingAnswers/:id', auth, checkAnswerExist, collectAnswers)

router.delete('/collectingAnswers/:id', auth, checkAnswerExist, uncollectAnswers)

module.exports = router